package view.login;

import common.RoomState;
import common.RoomTypes;
import model.booking.Booking;
import model.manager.Manager;
import model.room.Room;
import model.user.User;
import view.admin.Admin;
import view.user.Mainframe;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
/*
 * Created by JFormDesigner on Fri May 24 17:15:14 CST 2024
 */


/**
 * @author 孟晗
 */
public class Login extends JFrame {
    public Login() {
        initComponents();
        ButtonGroup bg = new ButtonGroup();;
        bg.add(UserButton);
        bg.add(ManegeButton);
    }

    private void Login(ActionEvent e) {
        // TODO add your code here

        String username=UserNameText.getText();
        String password=UserPasswdText.getText();
        User user;
        if((user = User.judge(username,password)) != null){
            JOptionPane.showMessageDialog(null,"登录成功");
            //跳转页面
            //根据我点击的JRadioButton判断是登录用户还是管理员并且进入相应的界面
            if(UserButton.isSelected()){
                System.out.println("测试........1");
                Mainframe mainframe=new Mainframe(user);
                mainframe.setVisible(true);
                this.dispose();
            }else if(ManegeButton.isSelected()){
                //if判断得出管理员对象，然后传给Admin界面
                if(username.equals("马小东")){
                    Manager manager=new Manager(username,password);
                    Admin admin=new Admin(manager);
                    admin.setVisible(true);
                    this.dispose();
                }
                else{
                    JOptionPane.showMessageDialog(null,"用户名或密码错误");
                }
            }
        }else{
            JOptionPane.showMessageDialog(null,"用户名或密码错误");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 马世鹏
        Loginbutton = new JButton();
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        UserPasswdText = new JTextField();
        UserNameText = new JTextField();
        label4 = new JLabel();
        UserButton = new JRadioButton();
        ManegeButton = new JRadioButton();

        //======== this ========
        setBackground(new Color(0xff33ff));
        var contentPane = getContentPane();

        //---- Loginbutton ----
        Loginbutton.setText("\u767b\u5f55");
        Loginbutton.setBackground(new Color(0x00ccff));
        Loginbutton.addActionListener(e -> Login(e));

        //---- label1 ----
        label1.setText("BULU\u56fd\u9645\u5927\u9152\u5e97\u6b22\u8fce\u60a8\uff01");
        label1.setBackground(Color.white);
        label1.setFont(new Font("\u65b9\u6b63\u7c97\u9ed1\u5b8b\u7b80\u4f53", Font.BOLD | Font.ITALIC, 24));

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d\uff1a");

        //---- label3 ----
        label3.setText("\u5bc6\u7801\uff1a");

        //---- label4 ----
        label4.setText("\u8bf7\u9009\u62e9\u8eab\u4efd:");

        //---- UserButton ----
        UserButton.setText("\u7528\u6237");

        //---- ManegeButton ----
        ManegeButton.setText("\u7ba1\u7406\u5458");

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(51, 51, 51)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label2, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(UserNameText, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
                                .addComponent(UserPasswdText, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
                            .addContainerGap(123, Short.MAX_VALUE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(label4, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(UserButton)
                                    .addGap(36, 36, 36)
                                    .addComponent(ManegeButton))
                                .addComponent(label1))
                            .addGap(0, 0, Short.MAX_VALUE))))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(154, 154, 154)
                    .addComponent(Loginbutton)
                    .addGap(0, 171, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(15, 15, 15)
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
                    .addGap(28, 28, 28)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(ManegeButton)
                        .addComponent(label4, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
                        .addComponent(UserButton))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED, 18, Short.MAX_VALUE)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                        .addComponent(UserNameText, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                        .addComponent(UserPasswdText, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                    .addGap(30, 30, 30)
                    .addComponent(Loginbutton)
                    .addGap(34, 34, 34))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 马世鹏
    private JButton Loginbutton;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField UserPasswdText;
    private JTextField UserNameText;
    private JLabel label4;
    private JRadioButton UserButton;
    private JRadioButton ManegeButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on

    public static void main(String[] args) throws FileNotFoundException {
        //反序列化用户信息
        String filename2 = "project/src/common/users.txt";  //文件路径
        List<User> users = new ArrayList<>();
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename2))) {
            users = User.getUsers().getClass().cast(in.readObject());
        } catch (IOException | ClassCastException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        User.setUsers(users);

        //反序列化房间信息
        String filename = "project/src/common/Rooms.txt";  //文件路径
        List<Room> x = new ArrayList<>();
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename))) {
            x = Room.getRooms().getClass().cast(in.readObject());
        } catch (IOException | ClassCastException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        Room.setRooms(x);

/*        //反序列化预约信息
        String filename3 = "project/src/common/Bookings.txt";  //文件路径
        List<Booking> bs = new ArrayList<>();
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename3))) {
            bs = Booking.getBookingList().getClass().cast(in.readObject());
        } catch (IOException | ClassCastException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        Booking.setBookingList(bs);
        for(Booking booking:bs){
            System.out.println(booking);
        }
        Booking.setBookingList(bs);*/

        new Login().setVisible(true);




    }
}